package [basepackage].service.job;

import java.lang.reflect.InvocationTargetException;
import java.util.ArrayList;
import java.util.List;

import org.apache.commons.beanutils.BeanUtils;
import org.apache.commons.collections.CollectionUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import lombok.extern.slf4j.Slf4j;
import [basepackage].bean.models.BaseJob;
import [basepackage].bean.models.BaseJobExample;
import [basepackage].bean.models.BaseJobExample.Criteria;
import [basepackage].dao.primary.BaseJobMapper;
import net.wicp.tams.common.constant.dic.YesOrNo;
import net.wicp.tams.common.micro.quartz.IScheduleJobStore;
import net.wicp.tams.common.micro.quartz.bean.ScheduleJob;

@Service
@Slf4j
public class ScheduleJobStoreImpl implements IScheduleJobStore {

	@Autowired
	private BaseJobMapper baseJobMapper;

	@Override
	public int deleteByPrimaryKey(Long jobId) {
		int ret = baseJobMapper.deleteByPrimaryKey(jobId);
		return ret;
	}

	@Override
	public int insert(ScheduleJob record) {
		BaseJob ret = new BaseJob();
		try {
			BeanUtils.copyProperties(ret, record);
		} catch (IllegalAccessException | InvocationTargetException e) {
			log.error("复制ScheduleJob属性失败", e);
		}
		int retvalue = baseJobMapper.insert(ret);
		return retvalue;
	}

	@Override
	public ScheduleJob selectByPrimaryKey(Long jobId) {
		BaseJob baseJob = baseJobMapper.selectByPrimaryKey(jobId);
		if (baseJob == null) {
			return null;
		}
		ScheduleJob ret = new ScheduleJob();
		try {
			BeanUtils.copyProperties(ret, baseJob);
		} catch (IllegalAccessException | InvocationTargetException e) {
			log.error("复制ScheduleJob属性失败", e);
		}
		return ret;
	}

	@Override
	public int updateByPrimaryKey(ScheduleJob record) {
		BaseJob ret = new BaseJob();
		try {
			BeanUtils.copyProperties(ret, record);
		} catch (IllegalAccessException | InvocationTargetException e) {
			log.error("复制ScheduleJob属性失败", e);
		}
		int retvalue = baseJobMapper.updateByPrimaryKey(ret);
		return retvalue;
	}

	@Override
	public List<ScheduleJob> getAll(YesOrNo isActiv) {
		BaseJobExample query = new BaseJobExample();
		Criteria createCriteria = query.createCriteria();
		if (isActiv != null) {
			createCriteria.andIsActivEqualTo(isActiv);
		}
		List<BaseJob> jobs = baseJobMapper.selectByExample(query);
		List<ScheduleJob> retlist = new ArrayList<>();
		for (BaseJob baseJob : jobs) {
			ScheduleJob temppo = new ScheduleJob();
			try {
				BeanUtils.copyProperties(temppo, baseJob);
			} catch (IllegalAccessException | InvocationTargetException e) {
				log.error("复制ScheduleJob属性失败", e);
			}
			retlist.add(temppo);
		}
		return retlist;
	}

	@Override
	public ScheduleJob selectByGroupAndName(String jobGroup, String jobName) {
		BaseJobExample query = new BaseJobExample();
		query.createCriteria().andJobGroupEqualTo(jobGroup).andJobNameEqualTo(jobName);
		List<BaseJob> jobs = baseJobMapper.selectByExample(query);
		if (CollectionUtils.isEmpty(jobs)) {
			return null;
		}
		if (jobs.size() > 1) {
			throw new IllegalStateException("数据异常,需要仅有一条数据被选中");
		}
		ScheduleJob ret = new ScheduleJob();
		try {
			BeanUtils.copyProperties(ret, jobs.get(0));
		} catch (IllegalAccessException | InvocationTargetException e) {
			log.error("复制ScheduleJob属性失败", e);
		}
		return ret;
	}

}
